//
//  DDCustomToolBar.swift
//  swift微博
//
//  Created by whitcast on 16/7/20.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
//按钮的不同类别  枚举
enum btnType: Int{
    case pic = 0
    case atSymbole = 1
    case about = 2
    case emoji = 3
    case add = 4
}

class DDCustomToolBar: UIToolbar {
    //保存按钮的数组
   private var buttonArr: [UIButton] = [UIButton]()
    //回调闭包
    var buttonClickClourse:((button: UIButton)->())?
    //构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //创建按钮
    private func creatToolbtn(imageName: String,buttonType: btnType)->UIButton{
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        btn.tag = buttonType.rawValue
//        btn.adjustsImageWhenHighlighted = false
        //按钮的点击事件
        btn.addTarget(self, action: "clickbtn:", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }
    //按钮的集合
    private func addButtons() {
        //添加按钮
        let picbtn = creatToolbtn("compose_toolbar_picture", buttonType: .pic)
        addSubview(picbtn)
        buttonArr.append(picbtn)
        let atBtn = creatToolbtn("compose_mentionbutton_background", buttonType: .atSymbole)
        addSubview(atBtn)
        buttonArr.append(atBtn)
        let aboutBtn = creatToolbtn("compose_trendbutton_background", buttonType: .about)
        addSubview(aboutBtn)
        buttonArr.append(aboutBtn)
        let emojiBtn = creatToolbtn("compose_emoticonbutton_background", buttonType: .emoji)
        addSubview(emojiBtn)
        buttonArr.append(emojiBtn)
        let addBtn = creatToolbtn("compose_add_background", buttonType: .add)
        addSubview(addBtn)
        buttonArr.append(addBtn)
    }
    //按钮的点击事件  通过button的tag值来判断进行的事件
    @objc private func clickbtn(button: UIButton){
        //点击了按钮
        print("点击了按钮")
        //闭包回调
        buttonClickClourse?(button: button)

    }
    
    //设置UI
    private func setupUI(){
        addButtons()
        //约束
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(buttonArr.count)
        
        for (i,value) in buttonArr.enumerate() {
            value.snp_makeConstraints(closure: { (make) -> Void in
                //左边距
                let leading = CGFloat(i) * width
                make.leading.equalTo(self).offset(leading)
                
                make.top.equalTo(self)
                
                make.bottom.equalTo(self)
                
                make.width.equalTo(width)
            })
        }
        
        
    }
    
    
}
